<script>
    // #ifdef H5
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return false;
    }
    // #endif
	export default {
        globalData: {
            version: 'v1.0.0',
            appName: '回客宝',
            appid: '',
            system: '',
            isWechat: true, //要求H5必须在微信中打开
            isSuccess: false,
            systemInfo: '',
            windowHeight: 0,
            windowWidth: 0,
            deviceType:'',
            loginInfo: {
            },
            hideAllMenus: ['menuItem:copyUrl', 'menuItem:share:email', 'menuItem:share:appMessage',
                'menuItem:share:timeline', 'menuItem:share:qq', 'menuItem:share:QZone', 'menuItem:share:weiboApp'
            ]
        },
		onLaunch: function() {
            let that = this
            uni.getSystemInfo({
                success(res) {
                    console.log('getSystemInfo',res)
                    that.globalData.systemInfo = res
                    that.globalData.deviceType=res.deviceType
                    that.globalData.windowHeight = res.screenHeight * (750 / res.windowWidth)
                    that.globalData.windowWidth = res.screenWidth * (750 / res.windowWidth)
                }
            })
            let loginInfoCache = uni.getStorageSync('userInfo')
            if (typeof loginInfoCache != Object && loginInfoCache != null && loginInfoCache != undefined &&
                loginInfoCache != '') {
            
                var loginInfo = loginInfoCache
                this.globalData.loginInfo = loginInfo
                // that.$u.vuex("vuex_userInfo", loginInfo);
            } else {
                var loginInfo = null
            }
            
            var appid = 'wx6733a5aae9712428' //回客
            this.globalData.appid = appid
            if (this.$debug) {
                //开发阶段判断是否为非微信环境，修改强制微信打开
                that.globalData.isWechat = (navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) ==
                    "micromessenger"
            }
            if (that.globalData.isWechat) {// &&!that.$debug
                
                //h5登陆
                var code = getQueryVariable('code'),
                    currentUrl = encodeURIComponent(window.location.href),
                    state = getQueryVariable('attach');
                console.warn('code:',code,loginInfo)
                if (!code) {
                    if (!loginInfo || loginInfo == '') {
                        window.location.href =
                            `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${currentUrl}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect `
                    }
                } else if (!loginInfo || loginInfo == '') {
                    that.$webLogin(code)
                }
                var wx = that.$wx
                that.$wx = that.$wx
                setTimeout(function() {
                    var jsApiList = ['updateTimelineShareData', 'updateAppMessageShareData',
                        'onMenuShareAppMessage', 'onMenuShareTimeline', 'scanQRCode', 'getLocation',
                        'openLocation', 'hideOptionMenu', 'hideMenuItems'
                    ];
                    that.$request(that.$api.wx.config, {
                        url: window.location.href
                    }, 'GET',false).then(res => {
                        let wxconfig = {
                            debug: false, //正式部署的时候记得把这个关掉 that.$debug
                            // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                            appId: res.data.appId, // 必填，公众号的唯一标识
                            timestamp: res.data.timestamp, // 必填，生成签名的时间戳
                            nonceStr: res.data.nonceStr, // 必填，生成签名的随机串,注意这个s是大写
                            signature: res.data.signature, // 必填，签名
                            //每增加一个要是用的接口，就需要在这里添加相应的微信js接口与之对应
                            jsApiList: res.data.jsApiList, // 必填，需要使用的JS接口列表
                            // openTagList: ['wx-open-launch-weapp'], // 可选，需要使用的开放标签列表，例如['wx-open-launch-app']
                        };
                        console.log(wxconfig)
                        uni.setStorageSync("wxconfig", wxconfig)
                        wx.config(wxconfig)
                        wx.ready(function() {
                            wx.hideMenuItems({
                                menuList: ['menuItem:copyUrl','menuItem:openWithQQBrowser','menuItem:openWithSafari',
                                    'menuItem:share:email'
                                ] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
                            });
                        })
                    })
                }, 1500)
            
            }
            console.log('App Launch',appid)
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
    @import "@/uni_modules/uview-ui/index.scss";
</style>
